package com.yisearch.search.doSearch.controller;


import com.alibaba.fastjson.JSONObject;
import com.yisearch.search.base.constant.ESConstant;
import com.yisearch.search.doSearch.service.DoSearchService;
import org.frameworkset.elasticsearch.boot.BBossESStarter;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 搜索商品控制类
 * @author yp
 * @date 2019/10/25
 * @param
 * @return
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    @Qualifier("productService")//指定接口实现类
    private DoSearchService productSerivce;

    @Autowired
    private BBossESStarter bBossESStarter;

    @RequestMapping("/import/fullData")
    public void importFullData() {
        ClientInterface client = bBossESStarter.getConfigRestClient(ESConstant.EsIndexName.SEARCH_PRODUCT_DSLMAPPING_PATH);
        try {
            boolean isexit = client.existIndice("product");
            if (!isexit) {
                client.createIndiceMapping("product", "createProductIndeice");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        productSerivce.importFullData(null);
    }

    /**
     * 一个简单检索
     *
     * @param key
     * @return
     */
    @RequestMapping("/search/{key}")
    public List search(@PathVariable String key) {
        Map params = new HashMap();
        Map ret = new HashMap();
        params.put("productdesc", key);
        ret = productSerivce.doSearch(params);
        return (List) ret.get("list");
    }


}
